package com.fanxuankai.boot.mqbroker.consumer.autoconfigure;

import com.fanxuankai.boot.mqbroker.MqBrokerDingTalkClientHelper;
import com.fanxuankai.boot.mqbroker.consumer.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author fanxuankai
 */
@Configuration
@EnableConfigurationProperties({MqBrokerConsumerProperties.class})
@ConditionalOnProperty(name = MqBrokerConsumerProperties.ENABLED, matchIfMissing = true)
public class MqBrokerConsumerAutoConfiguration {
    @Bean
    public EventListenerRegistry eventListenerRegistry(List<EventListenerContainer> containerList,
                                                       List<EventListener<?>> eventListenerList) {
        return new EventListenerRegistry(containerList, eventListenerList);
    }

    @Bean
    public AtLeastOnceEventDistributor atLeastOnceEventDistributor(EventListenerRegistry registry) {
        return new AtLeastOnceEventDistributor(registry);
    }

    @Bean
    public AtMostManyEventDistributor atMostManyEventDistributor(EventListenerRegistry registry) {
        return new AtMostManyEventDistributor(registry);
    }

    @Bean
    public AtMostOnceEventDistributor atMostOnceEventDistributor(EventListenerRegistry registry) {
        return new AtMostOnceEventDistributor(registry);
    }

    @Bean
    public DefaultEventDistributor defaultEventDistributor(EventListenerRegistry registry) {
        return new DefaultEventDistributor(registry);
    }

    @Bean
    public ConsumerTaskConfigurer consumerTaskConfigurer() {
        return new ConsumerTaskConfigurer();
    }

    @Bean
    public EventDistributorFactory eventDistributorFactory(List<AbstractEventDistributor> distributors) {
        return new EventDistributorFactory(distributors);
    }

    @Bean
    public MsgReceiveConsumer msgReceiveConsumer(@Autowired(required = false) MqBrokerDingTalkClientHelper helper) {
        return new MsgReceiveConsumer(helper);
    }

    @Bean
    public MsgReceiveService msgReceiveService() {
        return new MsgReceiveServiceImpl();
    }
}
